package com.huaxinkaiye.activity;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.huaxinkaiye.R;
import com.huaxinkaiye.application.HuaXinApplication;
import com.huaxinkaiye.bean.PictureInfo;
import com.huaxinkaiye.bean.RowsBean;
import com.huaxinkaiye.lookbigphoto.PhotosDialog;
import com.huaxinkaiye.utils.AlertDialogUtil;
import com.huaxinkaiye.utils.FileImageUpload;
import com.huaxinkaiye.utils.ImageUtils;
import com.huaxinkaiye.utils.OptionsUtil;
import com.huaxinkaiye.utils.PhotoImplActivity;
import com.huaxinkaiye.utils.ProgressUtil;
import com.huaxinkaiye.utils.SimpleClient;
import com.huaxinkaiye.utils.ToastUtil;
import com.huaxinkaiye.utils.UrlUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ChuKuDengJiActivity extends PhotoImplActivity implements
		OnClickListener, OnLongClickListener {
	private ImageView im_back, im_cancle, im_queren, im_xiugaikuwei,
			im_carchuku;
	public ImageView im_chepai, im_zuoqian, im_youhou, im_dajia;
	private RowsBean rowsBean;
	private ArrayList<NameValuePair> params;
	private TextView te_chepai, te_xinghao, te_yanse, te_xingzhi, te_zhuangtai,
			te_cangwei;
	public String localzqBigPicPath = "";
	public String localyhBigPicPath = "";
	public String localdjBigPicPath = "";
	public String localcpBigPicPath = "";
	public int zhaopianid;
	public ProgressUtil progress;
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				jiexiCukuData(msg.obj.toString());
				break;
			case 3:
				jiexiPictureData(msg.obj.toString());
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_chukudengji);
		progress = new ProgressUtil(this, "正在保存数据");
		initView();
		initData();
	}

	protected void jiexiCukuData(String string) {
		// TODO Auto-generated method stub
		Log.e("save_string", string);
		try {
			JSONObject json = new JSONObject(string);
			if (json.getBoolean("isSuccess")) {
				ToastUtil.show(this, json.getString("message"));
				Intent intent=new Intent(this,CarChuKuListActivity.class);
				setResult(RESULT_OK,intent);
				finish();
			} else {
				ToastUtil.show(this, json.getString("message"));
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void initView() {
		// TODO Auto-generated method stub
		im_back = (ImageView) findViewById(R.id.im_back);
		im_back.setOnClickListener(this);
		im_cancle = (ImageView) findViewById(R.id.im_cancle);
		im_cancle.setOnClickListener(this);
		im_queren = (ImageView) findViewById(R.id.im_queren);
		im_queren.setOnClickListener(this);
		im_chepai = (ImageView) findViewById(R.id.im_chepai);
		im_chepai.setOnClickListener(this);
		im_chepai.setOnLongClickListener(this);
		im_zuoqian = (ImageView) findViewById(R.id.im_zuoqian);
		im_zuoqian.setOnClickListener(this);
		im_zuoqian.setOnLongClickListener(this);
		im_youhou = (ImageView) findViewById(R.id.im_youhou);
		im_youhou.setOnClickListener(this);
		im_youhou.setOnLongClickListener(this);
		im_dajia = (ImageView) findViewById(R.id.im_dajia);
		im_dajia.setOnClickListener(this);
		im_dajia.setOnLongClickListener(this);
		im_xiugaikuwei = (ImageView) findViewById(R.id.im_xiugaikuwei);
		im_xiugaikuwei.setOnClickListener(this);
		im_carchuku = (ImageView) findViewById(R.id.im_carchuku);
		im_carchuku.setOnClickListener(this);
		te_chepai = (TextView) findViewById(R.id.te_chepai);
		te_xinghao = (TextView) findViewById(R.id.te_xinghao);
		te_yanse = (TextView) findViewById(R.id.te_yanse);
		te_xingzhi = (TextView) findViewById(R.id.te_xingzhi);
		te_zhuangtai = (TextView) findViewById(R.id.te_zhuangtai);
		te_cangwei = (TextView) findViewById(R.id.te_cangwei);
	}

	private void initData() {
		// TODO Auto-generated method stub
		rowsBean = (RowsBean) getIntent().getSerializableExtra("rowsBeanInfo");
		for (int i = 0; i < rowsBean.getPictures().size(); i++) {
			if (rowsBean.getPictures().get(i).getFileType() == 1) {
				localcpBigPicPath = UrlUtils.host
						+ rowsBean.getPictures().get(i).getFilepath();
				ImageLoader.getInstance().displayImage(localcpBigPicPath,
						im_chepai, OptionsUtil.optionsAvatar);
			}
			if (rowsBean.getPictures().get(i).getFileType() == 11) {
				localzqBigPicPath = UrlUtils.host
						+ rowsBean.getPictures().get(i).getFilepath();
				ImageLoader.getInstance().displayImage(localzqBigPicPath,
						im_zuoqian, OptionsUtil.optionsAvatar);
			}
			if (rowsBean.getPictures().get(i).getFileType() == 12) {
				localyhBigPicPath = UrlUtils.host
						+ rowsBean.getPictures().get(i).getFilepath();
				ImageLoader.getInstance().displayImage(localyhBigPicPath,
						im_youhou, OptionsUtil.optionsAvatar);
			}
			if (rowsBean.getPictures().get(i).getFileType() == 13) {
				localdjBigPicPath = UrlUtils.host
						+ rowsBean.getPictures().get(i).getFilepath();
				ImageLoader.getInstance().displayImage(localdjBigPicPath,
						im_dajia, OptionsUtil.optionsAvatar);
			}
		}
		// te_chepai,te_xinghao,te_yanse,te_xingzhi,te_zhuangtai,te_cangwei

		te_chepai.setText(rowsBean.getPlateNumber());
		te_xinghao.setText(rowsBean.getValueBrand().getModel());
		te_xingzhi.setText(rowsBean.getValueBrand().carType);
		te_zhuangtai.setText(rowsBean.getValueBrand().getDismantleType());
		te_cangwei.setText(rowsBean.getStoreCode());
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.im_back:
			Intent intent=new Intent(this,CarChuKuListActivity.class);
			setResult(RESULT_OK,intent);
			finish();
			break;
		case R.id.im_cancle:
			Intent intent2=new Intent(this,CarChuKuListActivity.class);
			setResult(RESULT_OK,intent2);
			finish();
			break;
		case R.id.im_chepai:
			// 查看大图
			if (localcpBigPicPath.equals("")) {
				ToastUtil.show(this, "没有图片");
				return;
			}
			lookBigImage(localcpBigPicPath);
			break;
		case R.id.im_zuoqian:
			// 查看大图
			if (localzqBigPicPath.equals("")) {
				ToastUtil.show(this, "没有图片");
				return;
			}
			lookBigImage(localzqBigPicPath);
			break;
		case R.id.im_youhou:
			// 查看大图
			if (localyhBigPicPath.equals("")) {
				ToastUtil.show(this, "没有图片");
				return;
			}
			lookBigImage(localyhBigPicPath);
			break;
		case R.id.im_dajia:
			// 查看大图
			if (localdjBigPicPath.equals("")) {
				ToastUtil.show(this, "没有图片");
				return;
			}
			lookBigImage(localdjBigPicPath);
			break;
		// case R.id.im_xiugaikuwei:
		// Intent intent = new Intent(this, CarRuKuDengJiActivity2.class);
		// intent.putExtra("rowsBeanInfo", rowsBean);
		// startActivityForResult(intent, 100);
		// break;
		case R.id.im_queren:
			AlertDialogUtil.showAlertDialog(this, "", "确定出库？",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							chuKu();
						}
					}, new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
			break;
		}
	}

	private void chuKu() {
		// TODO Auto-generated method stub
		if (HuaXinApplication.isNetWorkConnected()) {
			if (localdjBigPicPath.equals("")) {
				ToastUtil.show(this, "必须上传大架图");
				return;
			}
			params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("ids", rowsBean.getId() + ""));
			params.add(new BasicNameValuePair("status", rowsBean.getStatus()
					+ ""));
			new Thread() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					super.run();
					try {
						String data = SimpleClient.doPost(UrlUtils.chuku_url,
								params);
						Message mes = new Message();
						mes.what = 1;
						mes.obj = data;
						handler.sendMessage(mes);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}.start();
		} else {
			Toast.makeText(this, "网络异常", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	protected void lookBigImage(String s) {
		// TODO Auto-generated method stub
		final ArrayList<String> urllist = new ArrayList<String>();
		urllist.add(s);
		new PhotosDialog(ChuKuDengJiActivity.this, R.style.Dialog).show(0,
				urllist);
	}

	@Override
	public boolean onLongClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.im_chepai:
			zhaopianid = 1;
			showPopupMenu(im_chepai, false, "other");
			break;
		case R.id.im_zuoqian:
			zhaopianid = 11;
			showPopupMenu(im_zuoqian, false, "other");
			break;
		case R.id.im_youhou:
			zhaopianid = 12;
			showPopupMenu(im_youhou, false, "other");
			break;
		case R.id.im_dajia:
			zhaopianid = 13;
			showPopupMenu(im_dajia, false, "other");
			break;
		}
		return true;
	}

	@Override
	public void doAfterZoom(Bitmap bitmap) {
		// TODO Auto-generated method stub
		if (zhaopianid == 11) {
			im_zuoqian.setImageBitmap(bitmap);
		} else if (zhaopianid == 12) {
			im_youhou.setImageBitmap(bitmap);
		} else if (zhaopianid == 1) {
			im_chepai.setImageBitmap(bitmap);
		} else {
			im_dajia.setImageBitmap(bitmap);
		}
		String localPicPath = ImageUtils.savePicToCache(this, bitmap);
		Log.e("localPicPath", localPicPath + "---" + zhaopianid);
		params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("id", rowsBean.getId() + ""));
		params.add(new BasicNameValuePair("fileType", zhaopianid + ""));
		params.add(new BasicNameValuePair("pictureFile", localPicPath));
		final String url = UrlUtils.picture_url;
		if (HuaXinApplication.isNetWorkConnected()) {
			progress.show();
			new Thread() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					super.run();
					String result = FileImageUpload.postFile(url, params);
					Log.e("zyd_result", result);
					Message message = new Message();
					message.what = 3;
					message.obj = result;
					handler.sendMessage(message);
				}
			}.start();
		} else {
			Toast.makeText(this, "网络异常", Toast.LENGTH_SHORT).show();
		}
	}

	protected void jiexiPictureData(String string) {
		// TODO Auto-generated method stub
		Gson gson = new Gson();
		PictureInfo pinfo = gson.fromJson(string, PictureInfo.class);
		for (int i = 0; i < pinfo.getPictureInfo().size(); i++) {
			if (pinfo.getPictureInfo().get(i).getFileType() == 1) {
				localcpBigPicPath = UrlUtils.host
						+ pinfo.getPictureInfo().get(i).getPath();
				ImageLoader.getInstance().displayImage(localcpBigPicPath,
						im_chepai, OptionsUtil.optionsAvatar);
			}
			if (pinfo.getPictureInfo().get(i).getFileType() == 11) {
				localzqBigPicPath = UrlUtils.host
						+ pinfo.getPictureInfo().get(i).getPath();
				ImageLoader.getInstance().displayImage(localzqBigPicPath,
						im_zuoqian, OptionsUtil.optionsAvatar);
			}
			if (pinfo.getPictureInfo().get(i).getFileType() == 12) {
				localyhBigPicPath = UrlUtils.host
						+ pinfo.getPictureInfo().get(i).getPath();
				ImageLoader.getInstance().displayImage(localyhBigPicPath,
						im_youhou, OptionsUtil.optionsAvatar);
			}
			if (pinfo.getPictureInfo().get(i).getFileType() == 13) {
				localdjBigPicPath = UrlUtils.host
						+ pinfo.getPictureInfo().get(i).getPath();
				ImageLoader.getInstance().displayImage(localdjBigPicPath,
						im_dajia, OptionsUtil.optionsAvatar);
			}
		}
		progress.dismiss();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case 100:
				finish();
				break;
			}
		}
	}
}
